
************************************
*** FIGURE 5: Effect of EMR Adoption on Patient Adherence and Health

*** TABLE A12: Effect of EMR System on Patient-Level Outcomes
************************************

use Data/Analysis/Data_Individual, clear

gsort PatientId VisitDate
gen FVP=VisitDate if YearsPost>=0
by PatientId: egen FirstVisitPostDate=min(FVP)
gen FirstVisitPost=(FirstVisitPostDate==VisitDate)*(New==0)
gen LongRunPost=Post*(1-FirstVisitPost)*(New==0)

rename ViralSuppressed ViralSupp

*Drop early data where defaults are unknown
replace Default=. if VisitDate<EMRDate-90

local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit"
foreach y of varlist Default {
	reghdfe `y' New FirstVisitPost LongRunPost `covars', a(Datey VisitSite) cluster(VisitSite)
	estimates store e_AH`y'
	foreach x of varlist Male Female Age_* {
		reghdfe `y' New FirstVisitPost LongRunPost `covars' if `x'==1, a(Datey VisitSite) cluster(VisitSite)
		estimates store e_AH`y'_`x'
	}
}

local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit New"
foreach y of varlist Underweight ViralLoad ViralSupp {
	reghdfe `y' FirstVisitPost LongRunPost `covars', a(Datey VisitSite) cluster(VisitSite)
	estimates store e_AH`y'
	foreach x of varlist Male Female Age_* {
		reghdfe `y' FirstVisitPost LongRunPost `covars' if `x'==1, a(Datey VisitSite) cluster(VisitSite)
		estimates store e_AH`y'_`x'
	}
}

label var FirstVisitPost "First Visit"
label var LongRunPost "Long Run"

coefplot (e_AHDefault, label("All") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_AHDefault_Male, label("Male") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_AHDefault_Female, label("Female") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))) ///
, legend(rows(6)) keep(New FirstVisitPost LongRunPost)  xline(0, lpattern(dash) lcolor(black)) level(95) xtitle("Coefficient Estimates") ytitle("Post-EMR") xlabel(-0.3(0.1)0.1) 
	graph save "Output/Figures/Figure05PanelAi.gph", replace
	graph export "Output/Figures/Figure05PanelAi.png", replace

coefplot (e_AHUnderweight, label("All") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_AHUnderweight_Male, label("Male") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_AHUnderweight_Female, label("Female") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))) ///
, legend(rows(6)) keep(FirstVisitPost LongRunPost)  xline(0, lpattern(dash) lcolor(black)) level(95) xtitle("Coefficient Estimates") ytitle("Post-EMR") xlabel(-0.07(0.01)0.02) 
	graph save "Output/Figures/Figure05PanelBi.gph", replace
	graph export "Output/Figures/Figure05PanelBi.png", replace

coefplot (e_AHViralSupp, label("All") msymbol(O) mcol(black) ciopts(recast(rcap) lcol(black))) ///
(e_AHViralSupp_Male, label("Male") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_AHViralSupp_Female, label("Female") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))) ///
, legend(rows(6)) keep(FirstVisitPost LongRunPost) xline(0, lpattern(dash) lcolor(black)) ///
level(95) xtitle("Coefficient Estimates") ytitle("Post-EMR") ///
xlabel(-.15(.05).15, grid) graphregion(color(white))
graph save "Output/Figures/Figure05PanelCi.gph", replace
graph export "Output/Figures/Figure05PanelCi.png", replace

coefplot (e_AHDefault_Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_AHDefault_Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_AHDefault_Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_AHDefault_Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))) ///
, legend(rows(6)) keep(New FirstVisitPost LongRunPost)  xline(0, lpattern(dash) lcolor(black)) level(95) xtitle("Coefficient Estimates") ytitle("Post-EMR") xlabel(-0.3(0.1)0.1) 
	graph save "Output/Figures/Figure05PanelAii.gph", replace
	graph export "Output/Figures/Figure05PanelAii.png", replace

coefplot (e_AHUnderweight_Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_AHUnderweight_Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_AHUnderweight_Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_AHUnderweight_Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))) ///
, legend(rows(6)) keep(FirstVisitPost LongRunPost)  xline(0, lpattern(dash) lcolor(black)) level(95) xtitle("Coefficient Estimates") ytitle("Post-EMR") xlabel(-0.07(0.01)0.02) 
	graph save "Output/Figures/Figure05PanelBii.gph", replace
	graph export "Output/Figures/Figure05PanelBii.png", replace

coefplot (e_AHViralSupp_Age_09, label("0-9") msymbol(O) mcol(blue) ciopts(recast(rcap) lcol(blue))) ///
(e_AHViralSupp_Age_1017, label("10-17") msymbol(D) mcol(orange) ciopts(recast(rcap) lcol(orange))) ///
(e_AHViralSupp_Age_1849, label("18-49") msymbol(T) mcol(green) ciopts(recast(rcap) lcol(green))) ///
(e_AHViralSupp_Age_50plus, label("50+") msymbol(S) mcol(red) ciopts(recast(rcap) lcol(red))) ///
, legend(rows(6)) keep(FirstVisitPost LongRunPost) xline(0, lpattern(dash) lcolor(black)) ///
level(95) xtitle("Coefficient Estimates") ytitle("Post-EMR") ///
xlabel(-.35(.05).2, grid) graphregion(color(white))
graph save "Output/Figures/Figure05PanelCii.gph", replace
graph export "Output/Figures/Figure05PanelCii.png", replace



cap erase Output/Tables/TableA12.xls
cap erase Output/Tables/TableA12.txt

local date = 20240827
local covars "YearsInit Male Age_* UnderweightInit AdvHIVInit New"

foreach y of varlist Default Underweight ViralLoad ViralSupp{
	reghdfe `y' Post LongRunPost `covars', a(Datey VisitSite) cluster(VisitSite)
		outreg2 using Output/Tables/TableA12.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post LongRunPost) addtext(Year FE, Y, Clinic FE, Y, Controls, Y, Split, All) 

	foreach x of varlist Male Female Age_* {
		reghdfe `y' Post LongRunPost `covars' if `x'==1, a(Datey VisitSite) cluster(VisitSite)
			outreg2 using Output/Tables/TableA12.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post LongRunPost) addtext(Year FE, Y, Clinic FE, Y, Controls, Y, Split, `x') 

	}
}
